# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

# There's an ugliness issue in cyrus-sasl's build system.
# It builds libsasl2.dll with some compat objects, somehow
# gotten from saslauthd/Makefile.am?! but these files -
# getaddrinfo.c, getnameinfo.c (and one that doesn't ever
# get built, snprintf.c) depend on ws2_32 on Windows.
# The plugins need these objects too, so they are LN_S'ed
# as COMPAT_OBJS and that's added to plugins/Makefile.am:
# lib{plain,anonymous,kerberos4...}_la_{DEPENDENCIES,LIBADD}
# .. problem with doing it this way is that the ws2_32
# (encoded into LIB_SOCKET) dependency needs to be specified
# repeatedly for each one ..
# Instead these compat objects should be linked into a
# convenience libtool library (.a compiled with -fPIC) that
# carries the ws2_32 dependency with it, and this libtool
# library should be added as a dependency to each of the
# plugins. I've not fixed this because I've not got enough
# libtool experience to do it yet so instead 12-MinGW-w64..patch
# adds LIB_SOCKET all over the place in plugins/Makefile.am

_realname=cyrus-sasl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.1.26
pkgrel=5
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library"
arch=('any')
url="http://cyrusimap.web.cmu.edu/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gdbm"
         #"${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
options=('emptydirs' 'strip' '!makeflags')
source=(https://cyrusimap.org/releases/${_realname}-${pkgver}.tar.gz
        01-no-undefined.patch
        02-exeext.patch
        03-fix-plugins.patch
        04-manpage-paths.patch
        05-remove-warnings.patch
        06-fix-sqlite-search.patch
        07-fix-pkgconfig.patch
        08-cyrus-sasl-2.1.26-size_t.patch
        09-cyrus-sasl-2.1.26-msys2.patch
        10-MinGW-w64-already-has-getopt.patch
        12-MinGW-w64-uses-ws2_32-not-socket.patch
        13-MSYS2-Use-cp-f-instead-of-ln-s.patch
        14-MinGW-w64-add-LIBSASL_API-to-function-definitions.patch
        15-MinGW-w64-define-LIBSASL_EXPORTS_eq_1-for-sasldb.patch
        16-MinGW-w64-define-WIN32_LEAN_AND_MEAN-avoiding-handle_t-redef.patch
        17-MinGW-w64-define-S_IRUSR-and-S_IWUSR.patch
        18-MinGW-w64-use-windlopen.c-not-dlopen.c.patch
        19-paths-relocation.patch)
sha256sums=('8fbc5136512b59bb793657f36fadda6359cae3b08f01fd16b3d406f1345b7bc3'
            '70fb062a57c6b25c1687b9e8444641aaffc293dbec0bb6cb868579b5e98c2dd7'
            'cb670f740e81a2755875687df11d439367adca9767270ff1ed26fda3fd961818'
            '2f749dca67d6d21a96e8310ead76b2ad4d976df271579c3ef6e8c8dbb7ba5476'
            'a4abc51b2509e0792f7dc019afcb0bf6dacfdcf1132d28cf7562bbb18828a8fc'
            'b4687c44593fdd8b24f0054112014aadacf5e01e7e8dad65a4aa49c3ac4684c6'
            '35df432bf9067bcd182ba9aada4990ee2d891220c4c41fb42a194228544f70fb'
            '6177a9c28e60beeda650bcd51a01cfc36dc33669ff99bd0cd8b1c23a58550224'
            'b85b20bdd25b42098e07a8ba7e435f02b5cd882dcf69572c4d32de4a5e4f41bb'
            'f1b51575b1e8fdf34d8df806fd0fb53a13189fa197b43d57916d94d4be55416a'
            '7be58bc09e8fbf74c52522a12d12cdf47cbee8f660468e5c7968dc27e70fe981'
            '0c57c5fd167fc0f8104f6ac64216d654cf63e73c863cb837273ee031304fa566'
            '7424da7ad7d058ccc3a59f14e2401ab98191b1170bbf5eba2cf62055ffc56605'
            'c40ab13f6f9a6ea1dcd602e308a51caf162f9853821123a3f8fe88897eda3b98'
            'ed8b2a99098acd01a2006c3c4932189151e615380ce0c818cff4d481ac42cc94'
            '6ee8d8f19e786094bc9cdc6bc28b6d032733460fc535e94d4bdde0156f701175'
            '0fc7366f9231d445d6b8f2d3748e6df18e593d8f171b3b7858e848663023912b'
            'd64f15d637513ece131571324b78a6eb3db1fda41d77f174d7a5830f0052d180'
            '73d8d3416f7d1603af96532a651fb798f8c28982e248acf3fdc313569edfd1f6')

prepare() {
  cd ${srcdir}/$_realname-$pkgver

  patch -p1 -i ${srcdir}/01-no-undefined.patch
  patch -p1 -i ${srcdir}/02-exeext.patch
  patch -p1 -i ${srcdir}/03-fix-plugins.patch
  patch -p1 -i ${srcdir}/04-manpage-paths.patch
  patch -p1 -i ${srcdir}/05-remove-warnings.patch
  patch -p1 -i ${srcdir}/06-fix-sqlite-search.patch
  patch -p1 -i ${srcdir}/07-fix-pkgconfig.patch
  patch -p1 -i ${srcdir}/08-cyrus-sasl-2.1.26-size_t.patch
  patch -p1 -i ${srcdir}/09-cyrus-sasl-2.1.26-msys2.patch
  patch -p1 -i ${srcdir}/10-MinGW-w64-already-has-getopt.patch
  patch -p1 -i ${srcdir}/12-MinGW-w64-uses-ws2_32-not-socket.patch
  patch -p1 -i ${srcdir}/13-MSYS2-Use-cp-f-instead-of-ln-s.patch
  patch -p1 -i ${srcdir}/14-MinGW-w64-add-LIBSASL_API-to-function-definitions.patch
  patch -p1 -i ${srcdir}/15-MinGW-w64-define-LIBSASL_EXPORTS_eq_1-for-sasldb.patch
  patch -p1 -i ${srcdir}/16-MinGW-w64-define-WIN32_LEAN_AND_MEAN-avoiding-handle_t-redef.patch
  patch -p1 -i ${srcdir}/17-MinGW-w64-define-S_IRUSR-and-S_IWUSR.patch
  patch -p1 -i ${srcdir}/18-MinGW-w64-use-windlopen.c-not-dlopen.c.patch
  patch -p1 -i ${srcdir}/19-paths-relocation.patch

  sed 's/AM_CONFIG_HEADER/AC_CONFIG_HEADERS/' -i configure.in
  rm -f config/config.guess config/config.sub
  rm -f config/ltconfig config/ltmain.sh config/libtool.m4
  rm -fr autom4te.cache
  libtoolize -c
  aclocal -I config -I cmulocal
  automake -a -c
  autoheader
  autoconf

  pushd saslauthd
    rm -f config/config.guess config/config.sub
    rm -f config/ltconfig config/ltmain.sh config/libtool.m4
    rm -fr autom4te.cache
    libtoolize -c
    aclocal -I config -I ../cmulocal -I ../config
    automake -a -c
    autoheader
    autoconf
  popd
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  # Useful ref:
  # https://fedorapeople.org/cgit/elmarco/public_git/mingw32-cyrus-sasl.git/tree/mingw32-cyrus-sasl.spec
  # --oldincludedir=${MINGW_PREFIX}/include is so that /usr/include isn't added by default.
  # --enable-sql seems to have problems with <mysql.h> not existing, I thought it would be a part of sqlite.
  # --enable-sql
  # --without-saslauthd because it needs sockaddr_un.

  # We disable OpenSSL (--without-openssl --without-des) so that applications
  # with licenses incompatible with OpenSSL (such as GPL applications) do not
  # end up linked with OpenSSL if they use and redistribute Cyrus SASL outside
  # MSYS2. See these links:
  #
  #     * https://www.openssl.org/support/faq.html#LEGAL2
  #     * https://en.wikipedia.org/wiki/OpenSSL#Licensing

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-configdir=${MINGW_PREFIX}/etc/sasl2:${MINGW_PREFIX}/etc/sasl:${MINGW_PREFIX}/lib/sasl2 \
    --with-plugindir=${MINGW_PREFIX}/lib/sasl2 \
    --disable-static --enable-shared \
    --with-sqlite3=${MINGW_PREFIX} \
    --disable-ldapdb \
    --oldincludedir=${MINGW_PREFIX}/include \
    --without-saslauthd \
    --without-pwcheck \
    --without-openssl \
    --without-des \
    --without-authdaemond \
    --with-dblib=gdbm

  make
}

package() {
  local _plugindir=${MINGW_PREFIX}/lib/sasl2

  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install sasldir=${_plugindir}

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
